﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;

namespace UpdateSet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private string 目录;
        /// <summary>
        /// 选择目录并提前文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == folderBrowser.ShowDialog())
            {
                目录 = folderBrowser.SelectedPath;
                Find_File(目录);
            }
        }

        private void Find_File(string ml)
        {
            string[] files = System.IO.Directory.GetFiles(ml);
            string[] dir=Directory.GetDirectories(ml);

            //添加文件
            listView1.BeginUpdate();
            for(int i=0;i<files.Length;i++)
            {
             FileInfo f = new FileInfo(files[i]);
             ListViewItem item = new ListViewItem(f.Name);
             item.SubItems[0].Tag = f.FullName;
             item.SubItems.Add("");
             item.SubItems[1].Tag = f.Directory;
             item.SubItems.Add((f.Length/1024).ToString());
             listView1.Items.Add(item);
             Application.DoEvents();
            }
            listView1.EndUpdate();
            
            //查询目录
            for (int i = 0; i < dir.Length; i++)
            {
                //递归处理
                Find_File(dir[i]);
                Application.DoEvents();
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                Application.DoEvents();
                ListViewItem item = listView1.Items[i];
                string zipfile = item.SubItems[0].Tag.ToString();
                item.SubItems[1].Text = MD5(zipfile);
                long filesize=0;
                FileInfo finfo = new FileInfo(zipfile);
                filesize = finfo.Length;

                textBox1.AppendText(" <file name=\"" + System.IO.Path.GetFileName(zipfile)
                        + "\" path=\"" + item.SubItems[1].Tag.ToString().Trim().Replace(目录, "\\").Trim()
                        + "\" type=\"exe\" size=\"" + filesize.ToString()
                        + "\" md5=\"" + item.SubItems[1].Text.Trim()
                        + "\" actiontype=\"update\"/>\r\n");

                }
        }


        ///// <summary>
        ///// 添加zip压缩
        ///// </summary>
        ///// <param name="file"></param>
        ///// <returns></returns>
        //private string ZipFile(string file) {

        //    string zipfile = file.Substring(0, file.Length - (new FileInfo(file)).Extension.Length)+".zip";
        //    using (ZipOutputStream s = new ZipOutputStream(File.Create(zipfile)))
        //        {
        //            s.SetLevel(9);
        //            byte[] buffer = new byte[4096];

        //                using (FileStream fs = File.OpenRead(file))
        //                {

        //                    ZipEntry entry = new ZipEntry(Path.GetFileName(file));
        //                    entry.DateTime = DateTime.Now;
        //                    s.PutNextEntry(entry);

        //                    int sourceBytes;
        //                    do
        //                    {
        //                        sourceBytes = fs.Read(buffer, 0, buffer.Length);
        //                        s.Write(buffer, 0, sourceBytes);
        //                    } while (sourceBytes > 0);
        //                    fs.Flush();
        //                    fs.Close();
        //                }
        //             File.Delete(file);
        //            s.Finish();
        //            s.Close();
        //        }
        //    return zipfile;
        //}

        private string MD5(string file)
        {
            try
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                using (System.IO.Stream st = new System.IO.FileStream(file, System.IO.FileMode.Open))
                {
                    byte[] result = md5.ComputeHash(st);
                    st.Close();

                    StringBuilder sBuilder = new StringBuilder();

                    //判断md5是否一致
                    for (int i = 0; i < result.Length; i++)
                    {
                        sBuilder.Append(result[i].ToString("x2"));
                    }

                    // Return the hexadecimal string.
                    return sBuilder.ToString();

                    st.Close();
                }
            }
            catch
            {
                return string.Empty;
            }


        }
    }
}
